package com.bwsoft.api.action;

import java.io.IOException;
import java.util.Calendar;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.util.ValidatorUtil;

public class CodeAction extends ApiBaseAction<CodeAction> {
	private static Logger LOG = Logger.getLogger(CodeAction.class);

	private String email;
	private String phoneNumber;

	
	/**
	 * 2.5.2.3 邮箱修改密码向邮箱发送验证码
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void sendEmailCode() throws IOException, Exception {
		
		LOG.info("=================================开始调用【2.5.2.3 邮箱修改密码向邮箱发送验证码 sendEmailCode】==============================");
		
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		
		LOG.info("传入加密参数r="+r);
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();
		
		if(!parameMap.isEmpty()&&parameMap.containsKey("email")) this.setEmail((String) parameMap.get("email"));
		
       
		if(email==null||email.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "email不能为空！");
			canFlag=false;
		}
		
		if(canFlag&&!ValidatorUtil.isEmail(email)){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "请输入正确的邮箱！");
			canFlag=false;
		}
		
		//传入参数
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = codeService.sendMailCode(email);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}
		
		finishMethod();
		LOG.info("=================================调用结束【2.5.2.3 邮箱修改密码向邮箱发送验证码 sendEmailCode】==============================");

	}
	
	
	/**
	 * 2.5.2.1 手机号修改密码发送短信验证码
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void sendPhoneCode() throws IOException, Exception{
		LOG.info("=================================开始调用【2.5.2.1 手机号修改密码发送短信验证码 sendPhoneCode】==============================");
		try{
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		
		LOG.info("传入加密参数r="+r);
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();
		
		if(!parameMap.isEmpty()&&parameMap.containsKey("phoneNumber")) phoneNumber=((String) parameMap.get("phoneNumber"));
		
       
		if(phoneNumber==null||phoneNumber.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "phoneNumber不能为空！");
			canFlag=false;
		}
		
		if(canFlag&&!ValidatorUtil.isMobile(phoneNumber)){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "请输入正确的手机号！");
			canFlag=false;
		}
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = codeService.sendSmsCode(phoneNumber);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}}catch (Exception e) {
			responseJson.put("errcode", 5000);
			responseJson.put("errmsg", "服务器异常");
			responseJson.put("data", e.toString());
		}
		finishMethod();
		
		LOG.info("=================================调用结束【2.5.2.1 手机号修改密码发送短信验证码 sendPhoneCode】==============================");

		
	}
	
	
	
	/**
	 * 2.1.4 注册发送手机验证码
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void phoneRegister() throws IOException, Exception{
		LOG.info("=================================开始调用【2.1.4 注册发送手机验证码 phoneRegister】==============================");
		try{
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		
		LOG.info("传入加密参数r="+r);
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();
		
		if(!parameMap.isEmpty()&&parameMap.containsKey("phoneNumber")) phoneNumber=((String) parameMap.get("phoneNumber"));
		
       
		if(phoneNumber==null||phoneNumber.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "phoneNumber不能为空！");
			canFlag=false;
		}
		
		if(canFlag&&!ValidatorUtil.isMobile(phoneNumber)){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "请输入正确的手机号！");
			canFlag=false;
		}
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = codeService.phoneRegister(phoneNumber);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}}catch (Exception e) {
			responseJson.put("errcode", 5000);
			responseJson.put("errmsg", "服务器异常");
			responseJson.put("data", e.toString());
		}
		finishMethod();
		
		LOG.info("=================================调用结束【2.1.4 注册发送手机验证码 phoneRegister】==============================");

		
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
